<template>
  <div class="w-full h-50px" ref="chartRef"></div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';

  export default defineComponent({
    name: 'ProgressChart',
  });
</script>

<script lang="ts" setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import * as echarts from 'echarts';
  import { EChartsType } from 'echarts';

  const chartRef = ref();

  let chart: EChartsType | null = null;

  const resize = () => {
    (chart as EChartsType).resize();
  };

  onMounted(() => {
    chart = echarts.init(chartRef.value);

    const option = {
      xAxis: {
        show: false,
        min: 0,
        max: 100,
      },
      yAxis: {
        data: ['进度'],
        show: false, // 隐藏坐标轴
      },
      series: {
        type: 'bar',
        data: [78],
        // 柱条的宽度
        barWidth: 10,
        // 柱条的样式
        itemStyle: {
          color: 'pink',
        },
        // 是否显示背景色
        showBackground: true,
        // 提示文本
        label: {
          show: true,
          formatter: '|', // 文本内容
          color: 'pink', // 颜色
          position: 'right', // 位置
        },
      },
      grid: {
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
      },
    };

    chart.setOption(option);
    window.addEventListener('resize', resize);
  });

  onUnmounted(() => {
    window.removeEventListener('resize', resize);
  });
</script>
